/**
 * @author Jay
 */
$(function(){
	
	setSubCatePosition();
	setPopHeight();

	$('.category-item').mouseover(function(){
		CategoryPopShow(this);
		$(this).addClass('hover');
	});
	$('.category-item').mouseout(function(){
		countClear(this);
		$(this).removeClass('hover');
	});
	$('.subcategory-item dd a').live('mouseover',function(){
			subCateChildShow(this);
			$(this).addClass('hover');
	});
	$('.subcategory-item dd a').live('mouseout',function(){
		countClear(this);
		$(this).removeClass('hover');
	});
	
	$('body').mousemove(function(e){
		if(!isMouseOnCategory(e)){
			CategoryPophide();
		}
		
	});
	// 顶部登录时的回车提交效果
	$('#username,#password').keydown(function(e){
	  if(e.keyCode==13){
		 myUserLogin();  //处理事件
	  }
	});
	
});
/////////////////////////////////////////////
	var popIndex=0,popId='',subChildId='';
	var timeCount=0,		//计时
		timeMax=3,			//最大值
		timeInterval,
		timer=100;	
	//设置主菜单
	(function(){
		var _dom='<ul>';
		$.each(xesMenu,function(i){
			_dom+='<li class="category-item" id="cate_'+xesMenu[i].id+'"><a href="javascript:void(0);"><i></i>'+xesMenu[i].title+'</a></li>';
		});
		_dom+='</ul>';
		$('.category-items').html(_dom);
	})();
	//设置第一个子栏目
	(function(){
		var _id=$('.category-item').eq(0).attr('id').replace('cate_','');
		creatSubList(_id,0);
	})();
	//设置第一个pop右侧内容
	function setSubFirstChild(index){
		$('.subcategory-item dd a:first').addClass('on');
		var _subNode=$('.subcategory-item:eq(0)').find('dd a:eq(0)');
		if (_subNode.length > 0) {
			var _id = _subNode.attr('id');
			creatSubChild(_id.replace('subItem_', ''), index);
		}else{
			$('.subcategory-child').empty();
		}
	}
	//返回pop中的subCategory节点内容
	function getSub(id){
		var _s=xesCategory[id].cid;
		var _sub='';
		
		if(_s.length>0){
			$.each(_s,function(i){
				var _dom=xesCategory[_s[i]];
				_sub+='<dl class="subcategory-item">';
				
				if(_dom.id=='12-242'){	//如果id等于体验课程 则不显示“课程规划”的链接
					_sub+= '<dt id="sub_'+_dom.id+'"><em>'+_dom.title+'</em></dt>';
				}else{
					_sub+= '<dt id="sub_'+_dom.id+'"><a class="more" href="http://zt.xueersi.com/tg_tx/#'+_dom.id+'" target="_blank">'+_dom.title+'规划</a><em>'+_dom.title+'</em></dt>';
				}
				
				if (_dom.cid.length > 0) {
					_sub += ' <dd>';
					$.each(_dom.cid, function(j){
						var _node = xesCategory[_dom.cid[j]];
						_sub += '<a href="' + _node.url + '" id="subItem_' + _node.id + '">' + _node.title + '</a>';
					});
					_sub += '</dd>';
				}
				_sub+= '</dl>';
			});
			return _sub;
		}else{
			return false;
		}
	}
	//返回pop右侧subChild节点内容
	function getSubChild(id){
		var _c=xesCategory[id].cid;
		var _new='';
		var _html='<ul class="subcategory-child-items">';
		if (_c.length > 0) {
			$.each(_c, function(i){
				var _dom=xesCategory[_c[i]];
				if(_dom.ctm){
					if(isNewDate(_dom.ctm,7)){
						_new='<span style="color:#cc0000" t="'+_dom.ctm+'"> [新]</span>';
					}else{
						_new='';
					}

				}
				_html+='<li class="subcategory-child-item" id="subChildList_'+_dom.id+'"><a href="'+_dom.url+'">'+_dom.title+'</a>'+_new+'</li>';
			});
			_html+='</ul>';
			return _html;
		}else{
			return false;
		}
	}
	//设置高度
	function setPopHeight(){
		var _a=$('.subcategory-box').height();
		var _h=Math.max(_a,380);
		$('#xSubCategory').css({
			height:_h+24
		});

		$('.subcategory-bg').css({
			height:_h+4
		});
	}
	//创建subCategory
	function creatSubList(id,index){
		var _html='<h3>'+xesMenu[index].subtit+'</h3>';
        
        if (getSub(id)) {
            _html += getSub(id);
            $('.subcategory-box').attr('id', 'sub_' + id);
            $('.subcategory-items').html(_html);
        }
        else {
            $('.subcategory-items').html('');
        }

	}
	// 创建pop右侧节点
	function creatSubChild(id,index){
		var _html='<h4>'+xesMenu[index].hottit+'</h4>';
		var _id=subChildId||id;
        if (getSubChild(_id)) {
			_html+=getSubChild(_id);
			$('.subcategory-child').attr('id', 'subChild_' + _id);
            $('.subcategory-child').html(_html);
        }
        else {
            $('.subcategory-child-items').html('');
        }
	}
	// pop show
	function CategoryPopShow(obj){
		$(obj).addClass('hover');
		countStart(obj);
	}
	// pop hide
	function CategoryPophide(){
		setTimeout(function(){
			$('#xSubCategory').hide();
			$('.category-item').removeClass('hover');
			$('.category-item').removeClass('on');
		},timer*5);
		
		countClear();
	}
	//开始计时
	function countStart(o){
		var _id=o.id.replace('cate_',''),
			_index=$('.category-item').index(o);
		timeInterval=setInterval(function(){
			if(timeCount<timeMax){
				timeCount++;
				
			}else{
				popId=_id;
				popIndex=_index;
				creatSubList(_id,_index);
				setSubFirstChild(_index);
				$('#xSubCategory').show();
				setSubCatePosition();
				setPopHeight();
				$('.category-item').eq(_index).addClass('on').siblings('li').removeClass('on');
				countClear();
			}
		},timer);
	}
	//重置计时
	function countClear(){
		clearInterval(timeInterval);
		timeCount=0;
	}
	//捕获鼠标是否在菜单区域
	function isMouseOnCategory(e){
		var e=e||window.event;
		var x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		var y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		var cate=$('#xCategory');
		var sub=$('#xSubCategory');
		// 主菜单的上下左右边距
		var cL1=cate.offset().left,	cL2=cL1+cate.outerWidth();
			cT1=cate.offset().top,	cT2=cT1+cate.outerHeight();
		// pop的上下左右边距
		var sL1=sub.offset().left,	sL2=sL1+sub.outerWidth();
			sT1=sub.offset().top,	sT2=sT1+sub.outerHeight();
		if(x>cL1&&y>cT1&&x<cL2&&y<cT2||x>sL1&&x<sL2&&y>sT1&&y<sT2){
			return true;
		}else {
			return false;
		}
	}
	function setSubCatePosition(){
		$('#xSubCategory').css({
			top:$('#xCategory').offset().top+35,
			left:$('#xCategory').offset().left+160
		});
	}
	
	// 延迟显示四级菜单，并设置subcategory的当前状态
	function subCateChildShow(o){
		
		var _cateid=$('.subcategory-box').attr('id').replace('cate_','');
		var _cate=$('.category-items').find('li#cate_'+_cateid);
		var _eq=$('.category-item').index(_cate[0]);
		var _id=o.id;
			
		timeInterval=setInterval(function(){
			if(timeCount<timeMax){
				timeCount++;
			}else{
				creatSubChild(_id.replace('subItem_',''),_eq);
				setPopHeight();
				$('.subcategory-item dd a').removeClass('on');
				$('#'+_id).addClass('on');
				countClear();
			}
		},timer);
	}
/* oooooooooo focus scroll oooooooooo */
function focusScroll(o){
	this.ID=o.id||'';
	this.Num=o.node.length;
	this.Box=o.wrap;
	this.List=o.list;
	this.Nodes=o.node;
	this.NodeWidth=this.Nodes.outerWidth();
	this.LeftBtn=o.leftBtn;
	this.RightBtn=o.rightBtn;
	this.Btn=o.btn||'';
	this.AllWidth=this.Num*this.Nodes.outerWidth();
	this.Timer=o.timer||500;
	this.Interval;
}
focusScroll.prototype.setWidth=function(){
	this.List.width(this.Num*this.Nodes.outerWidth());
	this.NodeWidth=this.Nodes.outerWidth();
};
focusScroll.prototype.scrollStart=function(Direction){
	var _d=Direction||'left',_w,_left;
	_w=(_d=='left')?-(this.NodeWidth):this.NodeWidth;
	var _box=this.List;
	if(_d=='next'){
		this.List.animate({
			left:-this.NodeWidth
		},this.Timer,function(){
			_box.find(':first').appendTo(_box);
			_box.css({left:0});
		});
	}else{
		_box.find('li:last').prependTo(_box);
		_box.css({left:-this.NodeWidth});
		this.List.animate({
			left:0
		},this.Timer);
	}
};
focusScroll.prototype.start=function(d){
	d.setWidth();
	d.ID=setInterval(function(){
		d.scrollStart('next');
	},3000);
};

focusScroll.prototype.clear=function(d){
	clearInterval(d.ID);
};
focusScroll.prototype.click=function(d){
	d.LeftBtn.live('click',function(){
		d.scrollStart('prev');
	});
	d.RightBtn.live('click',function(){
		d.scrollStart('next');
	});
};
focusScroll.prototype.read = function(d) {
	d.setWidth();
	d.start(d);
	d.click(d);
};


/* oooooooooo focus scroll END oooooooooo */


/* oooooooooo about Date oooooooooo */
//获取当前格式化后的时间 yy-mm-dd
function getNowDate(){
    var day = new Date(), Year = 0, Month = 0, Day = 0, CurrentDate = "";
    //初始化时间   
    Year = day.getFullYear(), Month = day.getMonth() + 1, Day = day.getDate();
    CurrentDate += Year + "-";
    CurrentDate +=(Month >= 10)?(Month + "-"):("0" + Month + "-");
	CurrentDate +=(Day >= 10)?Day:("0" + Day);
    return CurrentDate;
}

/*
 * 用途：检查输入结束日期是否在今天之后，结束如期格式：yy-MM-dd
 * 结束如期>=起始日期
 * 输入： startDate：起始日期，字符串
 * 		 endDate：结束如期，字符串
 * 返回：如果通过验证返回true,否则返回false 
 * @param {Object} edate
 * @return {Boolean}
 */
function checkEndDate( edate ) {
	var today = getNowDate(); 
	var startDate = new Date(today.replace("-",",")).getTime() ;
	var endDate = new Date(edate.replace("-",",")).getTime() ;
	if( startDate > endDate ) {  return false;}
	return true;
}
/*
 * 是否距离今天days天之内 
 * 开始日期为传入的sdate
 * 结束日期为今天
 * sdate<=today并且相差值在num之内则返回true
 */
function isNewDate( sdate,num ) {
    var today = getNowDate();
    var startDate = new Date(sdate.replace(/-/g, ",")).getTime();
    var endDate = new Date(today.replace(/-/g, ",")).getTime();
    var days = compareDateDiff(sdate, today);
    if (endDate < startDate || days > num) {
        return false;
    }else{
		return true;	
	}
}
/* 计算（比较）两个日期的间隔天数
 * 
 * @param {Object} sDate1 (yy-mm-dd)
 * @param {Object} sDate2 (yy-mm-dd)
 */
function compareDateDiff(sDate1, sDate2){
    var aDate, oDate1, oDate2, iDays;
    aDate = sDate1.split("-");
    oDate1 = new Date(aDate[0],aDate[1],aDate[2]); 
    aDate = sDate2.split("-");
    oDate2 = new Date(aDate[0],aDate[1],aDate[2]);
	//把相差的毫秒数转换为天数
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24);
    return iDays;
}


/* oooooooooo about Date END oooooooooo */

